﻿using System;
using System.Linq;
using System.Collections.Generic;
using Domain.Core;
using Domain.MainModule.Entities;
using Domain.MainModule.Rates;
using Domain.MainModule.RetailRates;

namespace Application.MainModule.RateManagement
{
    public class RateManagementService : IRateManagementService, IDisposable
    {
        #region private properties

        private IRateRepository RateRepository
        {
            get;
            set;
        }

        private IRateRepository RateTypeRepository
        {
            get;
            set;
        }

        #endregion

        #region constructor

        /// <summary>
        /// Default constructor for RateService.
        /// </summary>
        /// <param name="rateRepository">RateRepository dependency, usually this is resolved by IoC</param>
        public RateManagementService(IRateRepository rateRepository)
        {
            if (rateRepository == (IRateRepository)null)
            {
                throw new ArgumentNullException(Resources.Messages.exception_DependenciesAreNotInitialized);
            }

            this.RateRepository = rateRepository;
        }

        #endregion

        #region IRateService implementations

        public void AddRate(Rate rate)
        {
            IUnitOfWork unitOfWork = this.RateRepository.UnitOfWork as IUnitOfWork;
            this.RateRepository.Add(rate);
            unitOfWork.Commit();
        }

        public void ChangeRate(Rate rate)
        {
            IUnitOfWork unitOfWork = this.RateRepository.UnitOfWork as IUnitOfWork;
            this.RateRepository.Modify(rate);
            unitOfWork.CommitAndRefreshChanges();
        }

        public Rate FindRateById(Guid rateId)
        {
            RateIdSpecification rateIdSpecification = new RateIdSpecification(rateId);
            return this.RateRepository.FindRate(rateIdSpecification);
        }

        public List<Rate> FindRateByCountryId(Guid countryId)
        {
            RateCountryIdSpecification rateCountryIdSpecification = new RateCountryIdSpecification(countryId);
            return this.RateRepository.FindRates(rateCountryIdSpecification).ToList<Rate>();
        }

        public void RemoveRate(Rate rate)
        {
            if (rate == (Rate)null)
                throw new ArgumentNullException("rate");

            IUnitOfWork unitOfWork = this.RateRepository.UnitOfWork as IUnitOfWork;
            this.RateRepository.Remove(rate);
            unitOfWork.CommitAndRefreshChanges();

        }

        #endregion

        #region IDisposable implementations

        /// <summary>
        /// Dispose associated context with this domain service.
        /// </summary>
        public void Dispose()
        {
            if (this.RateRepository != null
                &&
                this.RateRepository.UnitOfWork != null)
            {
                this.RateRepository.UnitOfWork.Dispose();
            }
        }

        #endregion
    }
}
